package com.triones.api.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.common.utils.SnCreator;
import com.triones.api.entity.WordOrderDO;
import com.triones.api.entity.param.WordOrderParams;
import com.triones.api.entity.vo.CustomWordOrderVO;
import com.triones.api.mapper.WordOrderMapper;
import com.triones.api.service.WordOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 工单 -业务实现
 */
@Service
@RequiredArgsConstructor
public class WordOrderServiceImpl implements WordOrderService {

    private final WordOrderMapper wordOrderMapper;

    @Override
    public TableDataInfo<CustomWordOrderVO> pageList(WordOrderParams wordOrderParams, PageQuery pageQuery) {
        Map<String, Object> params = wordOrderParams.getParams();
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("wo.sys_del",0);
        wrapper.and(ObjectUtil.isNotEmpty(wordOrderParams.getKeyword()),
            qw -> qw.like("wo.word_order_sn",wordOrderParams.getKeyword()).or()
                .like("wo.problem",wordOrderParams.getKeyword()).or()
                .like("wo.reply_content",wordOrderParams.getKeyword()));
        wrapper.eq(ObjectUtil.isNotEmpty(wordOrderParams.getStatus()),"wo.status",wordOrderParams.getStatus());
        wrapper.between(params.get("beginTime") != null && params.get("endTime") != null,
            "tn.create_time", params.get("beginTime"), params.get("endTime"));
        wrapper.orderByDesc("wo.create_time");
        wrapper.orderByAsc("wo.create_time");
        Page<CustomWordOrderVO> result = wordOrderMapper.selectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public CustomWordOrderVO getInfo(Long id) {
        return wordOrderMapper.selectDetailById(id);
    }

    @Override
    public boolean edit(WordOrderDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        WordOrderDO wordOrderDO = wordOrderMapper.selectById(dto.getId());
        if (wordOrderDO == null) {
            throw new ServiceException("当前工单不存在");
        }
        if (wordOrderDO.getStatus().equals("0")) {
            wordOrderDO.setReplyUserId(loginUser.getUserId());
            wordOrderDO.setReplyTime(System.currentTimeMillis());
            wordOrderDO.setStatus(dto.getStatus());
            wordOrderDO.setReplyContent(dto.getReplyContent());
        } else {
            wordOrderDO.setStatus(dto.getStatus());
        }
        return wordOrderMapper.updateById(wordOrderDO) > 0;
    }

    @Override
    public boolean sysRemove(Long id) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        WordOrderDO wordOrderDO = wordOrderMapper.selectById(id);
        if (wordOrderDO == null) {
            throw new ServiceException("当前工单不存在");
        }
        wordOrderDO.setReplyUserId(loginUser.getUserId());
        wordOrderDO.setReplyTime(System.currentTimeMillis());
        wordOrderDO.setSysDel("1");
        return wordOrderMapper.updateById(wordOrderDO) > 0;
    }

    @Override
    public boolean apiMemberRemove(Long id) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        WordOrderDO wordOrderDO = wordOrderMapper.selectById(id);
        if (wordOrderDO == null) {
            throw new ServiceException("当前工单不存在");
        }
        wordOrderDO.setMemberDel("1");
        return wordOrderMapper.updateById(wordOrderDO) > 0;
    }

    @Override
    public boolean apiAddWordOrder(WordOrderDO dto) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        dto.setWordOrderSn(SnCreator.getWordOrderCode());
        dto.setUid(loginUser.getUserId());
        dto.setCreateTime(System.currentTimeMillis());
        return wordOrderMapper.insert(dto) > 0;
    }

    @Override
    public TableDataInfo<CustomWordOrderVO> apiPageList(PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.eq("wo.member_del",0);
        wrapper.eq("wo.uid",loginUser.getUserId());
        wrapper.orderByAsc("wo.create_time");
        Page<CustomWordOrderVO> result = wordOrderMapper.selectPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }
}
